﻿<Page
    x:Name="pageRoot"
    x:Class="Posphorum.DietGroupedPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Posphorum"
    xmlns:common="using:Posphorum.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>

        <!--
            Collection of grouped items displayed by this page, bound to a subset
            of the complete item list because items in groups cannot be virtualized
        -->
        <CollectionViewSource
            x:Name="groupedItemsViewSource"
            Source="{Binding Groups}"
            IsSourceGrouped="true"
            ItemsPath="TotalFoodsInfo"/>

        <!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
        <Style x:Key="CaloriesTextStyle" TargetType="TextBlock" >
            <Setter Property="Foreground" Value="Gray" />
            <Setter Property="FontSize" Value="18" />
        </Style>

        <local:MealBlockDataTemplateSelector x:Key="mealBlockDataTemplateSelector" />
        <DataTemplate x:Key="mealBlockShortTemplate">
            <Grid Background="{Binding Mealtime, Converter={StaticResource mealtimeBrushConverter}}" Width="130" Height="130">
                <TextBlock Text="{Binding FoodInformations.Type}" FontSize="80" FontWeight="Light" Margin="14,-10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" />
                <TextBlock Text="{Binding FoodInformations.Kilocalories, Converter={StaticResource intCaloriesConverter}}" FontSize="13" VerticalAlignment="Bottom" Margin="10" HorizontalAlignment="Right" />
                <TextBlock Text="{Binding Mealtime, Converter={StaticResource mealtimeStringConverter}}" FontSize="15" VerticalAlignment="Top" Margin="19,80,0,0" HorizontalAlignment="Left" />
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="mealBlockTallTemplate">
            <Grid Background="{Binding Mealtime, Converter={StaticResource mealtimeBrushConverter}}" Width="130" Height="270">
                <TextBlock Text="{Binding FoodInformations.Type}" FontSize="80" FontWeight="Light" Margin="14,-10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" />
                <TextBlock Text="{Binding FoodInformations.Kilocalories, Converter={StaticResource intCaloriesConverter}}" FontSize="26" VerticalAlignment="Bottom" Margin="10" HorizontalAlignment="Right" />
                <TextBlock Text="{Binding Mealtime, Converter={StaticResource mealtimeStringConverter}}" FontSize="15" VerticalAlignment="Top" Margin="19,80,0,0" HorizontalAlignment="Left" />
            </Grid>
        </DataTemplate>
    </Page.Resources>

    <Page.BottomAppBar>
        <AppBar x:Name="bottomAppBar" Background="#FF00005D" BorderBrush="{StaticResource AppBarBorderThemeBrush}" IsSticky="True">
            <Grid>
                <AppBarButton Label="Clear" Icon="Clear" Click="ClearButtonClicked" />
                
                <AppBarButton Label="Refresh" Icon="Refresh" HorizontalAlignment="Right" Click="RefreshButtonClicked" />
            </Grid>
        </AppBar>
    </Page.BottomAppBar>
    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.ChildrenTransitions>
                <TransitionCollection>
                    <EntranceThemeTransition/>
                </TransitionCollection>
            </Grid.ChildrenTransitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="140"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <!-- Back button and page title -->
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="120"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Button x:Name="backButton" Margin="39,59,39,0" Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
                        Style="{StaticResource NavigationBackButtonNormalStyle}"
                        VerticalAlignment="Top"
                        AutomationProperties.Name="Back"
                        AutomationProperties.AutomationId="BackButton"
                        AutomationProperties.ItemType="Navigation Button"/>
                <TextBlock x:Name="pageTitle" x:Uid="DietPageTitle" Text="" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1" 
                        IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,30,40"/>
            </Grid>

            <!-- Horizontal scrolling grid used in most view states -->
            <SemanticZoom x:Name="semanticView" Grid.Row="1" Visibility="Collapsed">
                <SemanticZoom.ZoomedInView>
                    <local:CustomGridView
                        x:Name="itemGridView"
                        AutomationProperties.AutomationId="ItemGridView"
                        AutomationProperties.Name="Grouped Items"
                        Margin="0,-3,0,0"
                        Padding="116,0,40,46"
                        ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
                        SelectionMode="Multiple" SelectionChanged="itemGridView_SelectionChanged_1"
                        ItemTemplateSelector="{StaticResource mealBlockDataTemplateSelector}"
                        IsSwipeEnabled="True" ScrollViewer.IsHorizontalScrollChainingEnabled="False"
                        IsItemClickEnabled="True" ItemClick="ItemClicked">

                        <GridView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </GridView.ItemsPanel>
                        <GridView.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>
                                        <Grid Margin="1,0,0,6">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <Button Grid.Column="1" Style="{StaticResource SlimTextBlockButtonStyle}" Content="{Binding ServedDate, Converter={StaticResource dateConverter}, ConverterParameter=MM/DD-dayofweek}" Click="NavigateClicked" />
                                            <Rectangle Margin="0,10,10,10" Width="40" Height="40" Fill="{Binding ServedDate, Converter={StaticResource dayBrushConverter}}" HorizontalAlignment="Left"  />
                                        </Grid>
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>
                                <GroupStyle.Panel>
                                    <ItemsPanelTemplate>
                                        <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0" ItemHeight="140" ItemWidth="140"/>
                                    </ItemsPanelTemplate>
                                </GroupStyle.Panel>
                            </GroupStyle>
                        </GridView.GroupStyle>
                        <!--<GridView.ItemTemplate>
                            <DataTemplate>
                                <Grid>

                                </Grid>
                            </DataTemplate>
                        </GridView.ItemTemplate>-->
                    </local:CustomGridView>
                </SemanticZoom.ZoomedInView>
                <SemanticZoom.ZoomedOutView>
                    <GridView
                        x:Name="itemGridViewZoomedOut"
                        ScrollViewer.IsHorizontalScrollChainingEnabled="False"
                        Margin="0,-3,0,0"
                        Padding="116,0,116,40">
                        <GridView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VariableSizedWrapGrid ItemWidth="200" ItemHeight="300" MaximumRowsOrColumns="1" VerticalChildrenAlignment="Center"  />
                            </ItemsPanelTemplate>
                        </GridView.ItemsPanel>
                        <GridView.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="1,0,0,6">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Margin="3,-7,3,10" Text="{Binding Group.ServedDate, Converter={StaticResource dateConverter}, ConverterParameter=MM/DD-dayofweek}" Style="{StaticResource SubheaderTextBlockStyle}"/>
                                    <!--<Button Style="{StaticResource TextButtonStyle}" Content="{Binding Group.ServedDate, Converter={StaticResource dateConverter}, ConverterParameter=MM/DD-dayofweek}"/>-->
                                    <Rectangle Grid.Column="1" Margin="10" Width="120" Fill="{Binding Group.ServedDate, Converter={StaticResource dayBrushConverter}}" HorizontalAlignment="Left"  />
                                </Grid>
                            </DataTemplate>
                        </GridView.ItemTemplate>
                    </GridView>
                </SemanticZoom.ZoomedOutView>
            </SemanticZoom>

            <!-- Vertical scrolling list only used when snapped -->
            <ListView
                x:Name="itemListView"
                AutomationProperties.AutomationId="ItemListView"
                AutomationProperties.Name="Grouped Items"
                Grid.Row="1"
                Visibility="Collapsed"
                Margin="0,-10,0,0"
                Padding="10,0,0,60"
                ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
                SelectionMode="Multiple" IsItemClickEnabled="True"
                SelectionChanged="itemGridView_SelectionChanged_1"
                ItemClick="ItemClicked">
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel />
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>

                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <Grid Margin="7,7,0,0">
                                    <Rectangle Margin="5" Width="22" Height="22" Fill="{Binding ServedDate, Converter={StaticResource dayBrushConverter}}" HorizontalAlignment="Left"  />
                                    <Button Margin="35,2,0,0" Style="{StaticResource SlimTextBlockButtonStyle}" Content="{Binding ServedDate, Converter={StaticResource dateConverter}, ConverterParameter=MM/DD/dayofweek}" Click="NavigateClicked"/>
                                </Grid>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </ListView.GroupStyle>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid Height="60">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="54"/>
                                <ColumnDefinition Width="40"/>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Rectangle Margin="12,12,6,12" Fill="{Binding Mealtime, Converter={StaticResource mealtimeBrushConverter}}" />
                            <TextBlock Grid.Column="1" Margin="3,-7,3,0" Text="{Binding FoodInformations.Type}" FontSize="50" FontWeight="Light" VerticalAlignment="Top" HorizontalAlignment="Left" />
                            <TextBlock Grid.Column="2" Text="{Binding FoodInformations.Kilocalories, Converter={StaticResource intCaloriesConverter}}" FontSize="18" VerticalAlignment="Bottom" Margin="10,0,0,30" HorizontalAlignment="Left" />
                            <TextBlock Grid.Column="2" Text="{Binding Mealtime, Converter={StaticResource mealtimeStringConverter}}" FontSize="18" VerticalAlignment="Bottom" Margin="10" HorizontalAlignment="Left" />
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
        
        <Grid x:Name="TextListContainer" Visibility="Collapsed" Grid.Column="1" Width="346">
            <ListView x:Name="textList" Margin="10,30,40,30" SelectionMode="None" >
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel />
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="30" />
                                <RowDefinition Height="30" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <TextBlock Text="{Binding ServedDate, Converter={StaticResource dateConverter}, ConverterParameter=MM/DD/dayofweek}" Margin="5,10,0,0">
                                <TextBlock.Style>
                                    <Style BasedOn="{StaticResource BaseTextBlockStyle}" TargetType="TextBlock">
                                        <Setter Property="FontSize" Value="30"/>
                                        <Setter Property="FontWeight" Value="Light"/>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <TextBlock Grid.Row="1" Text="{Binding Mealtime, Converter={StaticResource mealtimeStringConverter}}" Margin="5,10,0,0">
                                <TextBlock.Style>
                                    <Style BasedOn="{StaticResource BaseTextBlockStyle}" TargetType="TextBlock">
                                        <Setter Property="FontSize" Value="25"/>
                                        <Setter Property="FontWeight" Value="Light"/>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>

                            <Grid Grid.Row="2" DataContext="{Binding FoodInformations}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding Type}" FontSize="70" Foreground="#FFEEEEEE" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                                <Grid Grid.Column="1">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <TextBlock Margin="13,10,0,0" Text="{Binding Kilocalories, Converter={StaticResource intCaloriesConverter}}" Style="{StaticResource CaloriesTextStyle}" />
                                    <ItemsControl ItemsSource="{Binding Foods}" Grid.Row="1"  Margin="13,0,0,20" >
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="Auto" />
                                                        <ColumnDefinition Width="*" />
                                                    </Grid.ColumnDefinitions>
                                                    <TextBlock Text="- " FontSize="18" />
                                                    <TextBlock Grid.Column="1" Text="{Binding langstr, Converter={StaticResource languageConverter}}" FontSize="18" TextWrapping="Wrap" />
                                                </Grid>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </Grid>
                            </Grid>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>

          
        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemGridView" Storyboard.TargetProperty="Padding">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="96,0,10,56"/>
                        </ObjectAnimationUsingKeyFrames>
                        
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextListContainer" Storyboard.TargetProperty="(Grid.Column)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextListContainer" Storyboard.TargetProperty="(Grid.Row)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextListContainer" Storyboard.TargetProperty="Width">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="NaN"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextListContainer" Storyboard.TargetProperty="Height">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="400"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="textList" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="10,30,10,100"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!--
                    The back button and title have different styles when snapped, and the list representation is substituted
                    for the grid displayed in all other view states
                -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemListView" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemGridView" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextListContainer" Storyboard.TargetProperty="(Grid.Column)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextListContainer" Storyboard.TargetProperty="(Grid.Row)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextListContainer" Storyboard.TargetProperty="Width">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="NaN"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextListContainer" Storyboard.TargetProperty="Height">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="400"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="textList" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="10,30,10,100"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</Page>
